package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.dao.GoodsPropertyValueMapper;
import com.jiangjiang.eshop.commodity.domain.GoodsPropertyValueEntity;
import com.jiangjiang.eshop.commodity.dto.GoodsPropertyValueDTO;
import com.jiangjiang.eshop.promotion.dto.GoodsPropertyValueConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName GoodsPropertyValueService
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/2 17:56
 * @Version 1.0
 **/
@Service
public class GoodsPropertyValueService extends ServiceImpl<GoodsPropertyValueMapper, GoodsPropertyValueEntity> {

    @Autowired
    private GoodsPropertyValueConvert goodsPropertyValueConvert;

    /**
     * 根据goodsId删除属性值数据
     * @param goodsId 商品id
     */
    public Boolean removeByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsPropertyValueEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsPropertyValueEntity::getGoodsId, goodsId);
        return this.remove(wrapper);
    }

    /**
     * 根据商品id查询商品属性值
     * @param goodsId 商品id
     * @return 查询结果
     */
    public List<GoodsPropertyValueDTO> queryByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsPropertyValueEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsPropertyValueEntity::getGoodsId, goodsId);
        return goodsPropertyValueConvert.convertEntities2DTOList(this.list(wrapper));
    }

    /**
     * 新增商品属性数据
     * @param propertyValues 商品属性数据
     * @return 处理结果
     */
    public Boolean batchSaveProperty(List<GoodsPropertyValueDTO> propertyValues) {
         List<GoodsPropertyValueEntity> entities = goodsPropertyValueConvert.convertDTOList2Entities(propertyValues);
        return this.saveBatch(entities);
    }

}
